package com.sisbibvir.unmsm.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sisbibvir.unmsm.entidades.TagsTesi;

public class Tag_TesisDAO extends DaoGenericImpl<TagsTesi, Integer> {

	public Tag_TesisDAO(EntityManager entityManager) {
		super(entityManager);
	}

	public List<TagsTesi> listarTodos() {
		entityManager.getTransaction().begin();
		@SuppressWarnings("unchecked")
		List<TagsTesi> aux = entityManager.createQuery(
				"SELECT p FROM TagsTesi p order by p.id").getResultList();
		entityManager.getTransaction().commit();
		return aux;
	}

	public int ObtenerId(String tag) {
		entityManager.getTransaction().begin();
		@SuppressWarnings("unchecked")
		List<TagsTesi> aux = entityManager.createQuery(
				"SELECT p FROM TagsTesi p where p.descripcion='" + tag + "'")
				.getResultList();
		entityManager.getTransaction().commit();
		int id = aux.get(0).getId();
		return id;
	}

	public TagsTesi getTesis(int id) {
		TagsTesi tagstesis = new TagsTesi();
		try {
			entityManager.getTransaction().begin();
			tagstesis = entityManager.find(
					com.sisbibvir.unmsm.entidades.TagsTesi.class, id);
		} catch (Exception e) {
			// log the exception
		}
		return tagstesis;
	}

	public int GenerarCodigo() {
		int iNuevoNumero = 1;

		try {
			entityManager.getTransaction().begin();
			Query q = entityManager
					.createQuery("Select max(tag.id) from TagsTesi tag");
			iNuevoNumero = Integer.parseInt(q.getSingleResult().toString()) + 1;
			entityManager.getTransaction().commit();
			entityManager.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return iNuevoNumero;
	}

	public List<String> ListarStringTags() {
		entityManager.getTransaction().begin();
		@SuppressWarnings("unchecked")
		List<String> aux =  entityManager.createQuery(
				"SELECT p.descripcion FROM TagsTesi p order by p.id").getResultList();
		entityManager.getTransaction().commit();
		entityManager.close();
		return aux;
	}

}
